package kr.hyunist.calendarproject.Adapter;

import java.util.ArrayList;
import java.util.Calendar;

import kr.hyunist.calendarproject.R;
import kr.hyunist.calendarproject.DAO.DAO;
import kr.hyunist.calendarproject.DTO.DayInfo;
import kr.hyunist.calendarproject.DTO.DayViewHolde;
import kr.hyunist.calendarproject.DTO.MyItem;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MonthCalendarAdapter extends BaseAdapter {
	private ArrayList<DayInfo> dayList;
	private ArrayList<MyItem> dayName;
	private Context context;
	private int resource;
	private LayoutInflater inflater;
	public DayViewHolde dayViewHolder;
	Calendar calendar;
	DAO dao;

	public MonthCalendarAdapter(Context context, int textResource, ArrayList<DayInfo> dayList) {
		this.context = context;
		this.dayList = dayList;
		this.resource = textResource;
		this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		dao = DAO.open(context);
	}
	
	public int getCount() {
		return dayList.size();
	}

	public Object getItem(int position) {
		return dayList.get(position);
	}

	public long getItemId(int position) {
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		DayInfo day = dayList.get(position);
		int pos = position;
		
		calendar = Calendar.getInstance();
		String cTime = String.format("%4d/%d/%d", calendar.YEAR, calendar.MONTH + 1, calendar.DAY_OF_MONTH);
		if (convertView == null) {
			convertView = inflater.inflate(resource, null);

			if (position % 7 == 6) {
				convertView.setLayoutParams(new GridView.LayoutParams(getCellWidthDP() + getRestCellWidthDP(), getCellHeightDP()));
			} else {
				convertView.setLayoutParams(new GridView.LayoutParams(getCellWidthDP(), getCellHeightDP()));
			}

			dayViewHolder = new DayViewHolde();
			
			dayViewHolder.background = (LinearLayout) convertView.findViewById(R.id.day_background);
			dayViewHolder.tvDay = (TextView) convertView.findViewById(R.id.day);
			dayViewHolder.tvPen = (ImageView) convertView.findViewById(R.id.schedulepen);
			dayViewHolder.tvPen.setVisibility(View.INVISIBLE);
			
			convertView.setTag(dayViewHolder);

		} else {
			dayViewHolder = (DayViewHolde) convertView.getTag();
		}

		if (day != null) {
			dayViewHolder.tvDay.setText(String.valueOf(day.getDay()));

			if (day.isInMonth()) {
				if (position % 7 == 0) {
					dayViewHolder.tvDay.setTextColor(Color.RED);
				} else if (position % 7 == 6) {
					dayViewHolder.tvDay.setTextColor(Color.BLUE);
				} else {
					dayViewHolder.tvDay.setTextColor(Color.BLACK);
				}
			} else {
				dayViewHolder.tvDay.setTextColor(Color.GRAY);
			}
		}
		
		//정보가 있는 날짜에 표시 기능			
/*		dayName = dao.selectTodayScheduleFromDate(cTime);
		String dName = String.valueOf(day.getYear() + "/" + day.getMonth() + "/" + day.getDay());
		String[] mDate = new String[42];
		
		if(dayName.get(position).getStartDate().equals(dName)){ 
			dayViewHolder.tvPen.setVisibility(View.VISIBLE);
		} else {
			dayViewHolder.tvPen.setVisibility(View.INVISIBLE);
		}*/
		
/*		if(position < dateList.size()){
			mDate[position] = dateList.get(position).getStartDate();

		} else {
			mDate[position] = dateList.get((position - (pos - dateList.size())) - 1).getStartDate();
		}
		*/

		
		return convertView;
	}

	private int getCellWidthDP() {
		int width = context.getResources().getDisplayMetrics().widthPixels;
		int cellWidth = width / 7;

		return cellWidth;
	}

	private int getRestCellWidthDP() {
		int width = context.getResources().getDisplayMetrics().widthPixels;
		int cellWidth = width % 7;

		return cellWidth;
	}

	private int getCellHeightDP() {
		int height = context.getResources().getDisplayMetrics().widthPixels;
		int cellHeight = height / 6;

		return cellHeight;
	}
		
}
